package org.jiamandan.com.utils;


import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.StringUtils;
import org.jiamandan.com.dto.YXBasicDto;

import java.util.Calendar;
import java.util.Date;

/**
 * @version Revision 1.0.1
 * @版权：版权所有 (c) 2021
 * @author:Jmd
 * @see:
 * @创建日期：2023-03-23 14:37
 * @功能说明：
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2023-03-23 14:37	         Jmd        	创建
 * @end
 */
public class YxObjectUtil {

    private static final int[] number = {2, 2, 4, 2, 2, 4};

    public static YXBasicDto parse(String payload) {
        YXBasicDto dto = new YXBasicDto();
        int cursor = 0, length = payload.length();
        int start = 0, end =  number[cursor];
        dto.setStartPrefix(StringUtils.substring(payload, start, end));
        start = end;
        end += number[++cursor];
        dto.setDataLength(StringUtils.substring(payload, start, end));
        start = end;
        end += number[++cursor];
        dto.setSequence(StringUtils.substring(payload, start, end));
        start = end;
        end += number[++cursor];
        dto.setEncodeFlag(StringUtils.substring(payload, start, end));
        start = end;
        end += number[++cursor];
        dto.setFrameType(StringUtils.substring(payload, start, end).toUpperCase());
        dto.setValidate(StringUtils.substring(payload, length - 4, length));
        start = end;
        dto.setData(StringUtils.substring(payload, start, length - 4));
        return dto;
    }
    /**
     * 嘉满电-YX应答拼接报文
     *
     * @param payload
     * @return
     */
    public static String replyMessage(String payload,String sequence, String cmd) {
        //“序列号域+加密标志(固定：00)+帧类型标志(cmd)+消息体”
        StringBuilder content = new StringBuilder(sequence)
                .append("00")
                .append(StringUtils.leftPad(cmd, 2, "0"))
                .append(payload);
        StringBuilder builder = new StringBuilder();
        builder.append("68")
                .append(StringUtils.leftPad(Integer.toHexString(content.length() / 2), 2, "0"))
                .append(content.toString())
                .append(CommonUtil.getCRC(content.toString()));
        return builder.toString();
    }
    /**
     * 嘉满电-YX下发拼接报文
     *
     * @param payload
     * @return
     */
    public static String pushMessage(String payload,String cmd) {
        //“序列号域+加密标志(固定：00)+帧类型标志(cmd)+消息体”
        StringBuilder content = new StringBuilder(CommonUtil.reverseHex(StringUtils.leftPad("0001",4,"0")))
                .append("00")
                .append(StringUtils.leftPad(cmd, 2, "0"))
                .append(payload);
        StringBuilder builder = new StringBuilder();
        builder.append("68")
                .append(StringUtils.leftPad(Integer.toHexString(content.length() / 2), 2, "0"))
                .append(content.toString())
                .append(CommonUtil.getCRC(content.toString()));
        return builder.toString();
    }



//    public static void main(String[] args) {
//        //M << n = M * 2n
//        // M >> n = M / 2n
//
////        String content = "000000015503141278230500020A56342E312E353000010101010101010101010104";
//        System.out.println(getCP56time2a2Time("38c7360e0c0417"));
//        System.out.println(getTime2CP56time2a(new Date()));
////        System.out.println(getTime2CP56time2a(DateUtil.parse("2020-03-16 17:14:47",DateUtil.DATETIME_FORMAT)));
////        System.out.println(CommonUtil.toHexString("11000015"));
//    }

    public static String getTime2CP56time2a(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        StringBuilder builder = new StringBuilder();
        builder.append(StringUtils.leftPad(Integer.toHexString(Integer.valueOf(StringUtils.substring(calendar.get(Calendar.YEAR) + "", 2, 4))), 2, '0'));
        builder.append(StringUtils.leftPad(Integer.toHexString(calendar.get(Calendar.MONTH) + 1), 2, '0'));
        builder.append(StringUtils.leftPad(Integer.toHexString(calendar.get(Calendar.DAY_OF_MONTH)), 2, '0'));
        builder.append(StringUtils.leftPad(Integer.toHexString(calendar.get(Calendar.HOUR_OF_DAY)), 2, '0'));
        builder.append(StringUtils.leftPad(Integer.toHexString(calendar.get(Calendar.MINUTE)), 2, '0'));
        builder.append(StringUtils.leftPad(Integer.toHexString(calendar.get(Calendar.SECOND) * 1000), 4, '0'));
        String str = CommonUtil.reverseHex(builder.toString());
        return str;
    }

    public static String getCP56time2a2Time(String str) {
        return "20" + Integer.parseInt(str.substring(12, 14), 16) + "-" + Integer.parseInt(str.substring(10, 12), 16)
                + "-" + Integer.parseInt(str.substring(8, 10), 16) + " " + Integer.parseInt(str.substring(6, 8), 16)
                + ":" + Integer.parseInt(str.substring(4, 6), 16) + ":"
                + Integer.parseInt(str.substring(2, 4) + "" + str.substring(0, 2), 16) / 1000;
    }

    public static String stringToAsciiToHex(String str){
        StringBuilder hex = new StringBuilder();
        char[] chars = str.toCharArray();
        for (char aChar:chars) {
            hex.append(Integer.toHexString((int)aChar));
        }
        return hex.toString();
    }
}
